package bbs.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.xkh.xkh.R;

import java.util.List;

import bbs.activity.OneSubjectActivity;
import bbs.adapter.MainViewAdapter;
import bbs.model.Subject;
import bbs.net.AllSubject;

/**
 * 所有的主题
 * Created by nimi on 2015/11/11.
 */
public class SubjectsFragment extends Fragment implements View.OnClickListener,AllSubject.Delegate {
    private View rootView;
    private AllSubject allSubject=new AllSubject();
    private List<Subject> subjectList;
    private MainViewAdapter subjectAdapter;
    //Subject subject;
    //点击事件的回调
    public interface OneSubjectClickListener
    {
        void oneSubjectClickListener();
    }

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //allSubject.get();

    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            allSubject.get();
            allSubject.setDelegate(SubjectsFragment.this);
            rootView = inflater.inflate(R.layout.bbs_board, null);
            //ListView listView = (ListView) rootView.findViewById(R.id.subjectlist);
            //listView.setAdapter(subjectAdapter);
        }
        return rootView;
    }

    //宿主activity处理点击事件
    @Override
    public void onClick(View v)
    {
        if (getActivity() instanceof OneSubjectClickListener)
        {
            ((OneSubjectClickListener) getActivity()).oneSubjectClickListener();
        }
    }

    public void onDestroyView()
    {
        // TODO Auto-generated method stub
        super.onDestroyView();
    }
    public void onResponse(String response,AllSubject allSubject){
        if(allSubject.getDownload().getStatus()==1) {
            subjectList=allSubject.getDownload().getTopics();
            MainViewAdapter subjectAdapter = new MainViewAdapter(getActivity(), subjectList);
            ListView listView = (ListView)rootView.findViewById(R.id.subjectlist);
            listView.setAdapter(subjectAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Subject subject = subjectList.get(position);
                    Intent intent=new Intent(getActivity(), OneSubjectActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putLong("subjectID",subject.getTopic_ID());
                    bundle.putString("subjectNAME", subject.getTopic_Name());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            System.out.print(subjectList);
        }

    }
    public void onErrorResponse(VolleyError error,AllSubject allSubject){

    }
}
